﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cineplex.Contacts.Models;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;
namespace Cineplex.Contacts.Controllers
{    
    public class ContactController : Controller
    {
        private readonly ContactDataModel _dataContext = new ContactDataModel();

        //
        // GET: /Contact/

        public ActionResult Index()
        {
            using (_dataContext)
            {
                return View(_dataContext.DataModel.Contacts.ToList());    
            }            
        }

        [GridAction]
        public ActionResult AjaxIndex()
        {
            using (_dataContext)
            {
                return View( new GridModel(_dataContext.DataModel.Contacts.ToList()));
            }
        }

        //
        // GET: /Contact/Details/5

        public ViewResult Details(int id)
        {
            using (_dataContext)
            {
                return View(_dataContext.GetContact(id));
            }
        }

        //
        // GET: /Contact/Create

        public ActionResult Create()
        {
            //ViewBag.Message = "Member registration!";
            return View();
        } 

        //
        // POST: /Contact/Create

        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            if (ModelState.IsValid)
            {
                using (_dataContext)
                {
                    _dataContext.AddContact(contact);
                }
                return RedirectToAction("Index");  
            }

            return View(contact);
        }
        
        //
        // GET: /Contact/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewData["mode"] = "popup";
            using (_dataContext)
            {
                return View(_dataContext.GetContact(id));    
            }            
        }

        [GridAction]
        public ViewResult AjaxEdit(int id)
        {
            using (_dataContext)
            {
                return View( _dataContext.GetContact(id));
            }
        }

        //
        // POST: /Contact/Edit/5

        [HttpPost]
        public ActionResult Edit(Contact contact)
        {
            if (ModelState.IsValid)
            {
                using (_dataContext)
                {
                    _dataContext.EditContact(contact);
                }
                return RedirectToAction("Index");
            }
            return View(contact);
        }

        //
        // GET: /Contact/Delete/5
 
        public ActionResult Delete(int id)
        {

            using (_dataContext)
            {
                return View(_dataContext.GetContact(id));    
            }            
        }

        [GridAction]
        public ActionResult AjaxDelete(int id)
        {

            using (_dataContext)
            {
                return View(_dataContext.GetContact(id));
            }
        }

        //
        // POST: /Contact/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            using (_dataContext)
            {
                _dataContext.Delete(id);
            }
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _dataContext.Dispose();
            base.Dispose(disposing);
        }
    }
}